<script setup lang="ts">

import {ref, onMounted} from 'vue'
import { Search,ArrowDown } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { ElTable } from 'element-plus'
import { ElButton, ElDialog } from 'element-plus'
import router from '@/router'
import { GetUser } from "@/utils/lStorage"

import {formatCreateTime} from "@/utils/format"
import {apiConfigList,apiConfigDelete,apiConfigUpdate,apiConfigSetDefault} from '@/api/api'
import {formatAnalyzeStatus,formatTagAnalyzeStatus,formatSkillType,formatTagSkillType,formatSkillStatus,formatTagSkillStatus} from '@/utils/format'


const searchKey = ref({
  name :"",
  desc :"",
  // skillStatus : 0,
})
const pageConfig = ref({
  pageNum : 1,
  pageSize :15,
  total:0,

})

const skillIsDefalut = [
  {
    value: "Y",
    label: '是',
  },
  {
    value: "N",
    label: '否',
  },
] 

const showDetail =ref({
  openFlag:false,
  detail:{
    Config:"",
  },
})

const updateParmas =ref({
  openFlag:false,
  detail:{},
})

const configList = ref([])

async function handlerConfigList() {
  const req = {
    pageNum:pageConfig.value.pageNum,
    pageSize:pageConfig.value.pageSize,
  }
  if (searchKey.value.name){
    req.Name  = searchKey.value.name
  }
  if (searchKey.value.desc){
    req.Desc  = searchKey.value.desc
  }
  if (searchKey.value.isDefalut) {
    req.IsDefalut = searchKey.value.isDefalut
  }

  const data = await apiConfigList(req)
  if (data.CODE != "ok") {
      console.log(data)
      ElMessage.error("执行失败， 请联系管理员")
      return 
  }

  pageConfig.value.pageNum = data.DATA.PageNumber
  pageConfig.value.pageCount = data.DATA.PageCount
  pageConfig.value.pageSize = data.DATA.PageSize
  pageConfig.value.total = data.DATA.TotalCount
  configList.value = data.DATA.TaskConfigList
}



const openConfigUpdate = (item) => {
  updateParmas.value.openFlag = true
  updateParmas.value.detail = item
}

async function handlerConfigUpdate()  {
  
  const data = await  apiConfigUpdate(updateParmas.value.detail)
  if (data.CODE != "ok") {
      console.log(data)
      ElMessage.error("执行失败， 请联系管理员")
      return 
  }
    ElMessage({
      message: '操作成功！',
      type: 'success',
    })
    updateParmas.value.openFlag= false 
}


const handleConfigDetail = (item) => {
  showDetail.value.openFlag = true
  showDetail.value.detail = item
}
const handleConfigCreate = () => {
  router.push({ path: '/config/create'})
}

async function handlerConfigSetDefault(item)  {
  const req= {
    ConfigId: item.ConfigId,
  }
  const data = await  apiConfigSetDefault(req)
  if (data.CODE != "ok") {
      console.log(data)
      ElMessage.error("执行失败， 请联系管理员")
      return 
  }
    ElMessage({
      message: '操作成功！',
      type: 'success',
    })
}

async function handlerConfigDelete(item)  {
  if (item.IsDefault == 'Y') {
      ElMessage.error("默认配置不允许删除")
      return 
  }

  const req= {
    ConfigId: item.ConfigId,
  }
  const data = await  apiConfigDelete(req)
  if (data.CODE != "ok") {
      console.log(data)
      ElMessage.error("执行失败， 请联系管理员")
      return 
  }

    ElMessage({
      message: '操作成功！',
      type: 'success',
    })
  handlerConfigList()
    
}



onMounted(() => {
  handlerConfigList()
})

</script>

<template>
  
  <el-row :gutter="20" justify="space-between" >
    <el-col :span="8">
      <el-button type="primary" @click="handleConfigCreate" >添加</el-button>
    </el-col>
    <el-col :span="16">
      <el-row justify="end" :gutter="10">
        <el-col  :span="4"> 
            <el-input v-model="searchKey.name" placeholder="名称" clearable />
        </el-col>
        <el-col  :span="6"> 
            <el-input v-model="searchKey.desc" placeholder="说明" clearable />
        </el-col>
        <el-col  :span="4"> 
            <el-select v-model="searchKey.isDefalut"  placeholder="默认" clearable >
            <el-option
              v-for="status in skillIsDefalut"
              :key="status.value"
              :label="status.label"
              :value="status.value"
            />
          </el-select>
        </el-col>
        <el-col :span="2"> 
          <el-button   class='pagination' type="primary" :icon="Search" @click="handlerConfigList" >查询</el-button>
        </el-col>
      </el-row>
    </el-col>

  </el-row>

<div class="info_box">
  <el-table :data="configList" style="width: 100%">
    <el-table-column prop="Name" label="名称" width="300" />
    <el-table-column prop="Desc" label="说明"  />
    <el-table-column prop="IsDefault" label="默认"   width="60" >

    </el-table-column>


    <el-table-column prop="CreateTime" label="创建时间" :formatter="formatCreateTime" width="180" />

    <el-table-column label="操作" width="350" >
        <template  #default="item">
          <el-button link type="primary" v-show="item.row.IsDefault=='N'"   @click="handlerConfigSetDefault(item.row)">设置默认</el-button>
          <el-button link type="primary" @click="handleConfigDetail(item.row)">详情</el-button>
          
          <el-button link  @click="openConfigUpdate(item.row)"  >编辑</el-button>
          <el-button link type="danger" @click="handlerConfigDelete(item.row)"  >删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  
  <el-pagination class='pagination' size="large" :page-size="pageConfig.pageSize" layout=" prev, pager, next, total" :total="pageConfig.total"
      v-model:current-page="pageConfig.pageNum" @current-change="handlerSkillList" />

</div>
  <el-drawer v-model="showDetail.openFlag" title="详情" :with-header="true">
    {{showDetail.detail.Config}}
  </el-drawer>

  <el-drawer v-model="updateParmas.openFlag" title="更新" :with-header="true">
  <div class="info_box">
    
  <el-form :model="updateParmas" label-width="120px">
    <el-form-item label="配置名称">
      <el-input v-model="updateParmas.detail.Name" clearable/>
    </el-form-item>

  <el-form-item label="说明">
      <el-input v-model="updateParmas.detail.Desc"     
        autosize
        clearable
      type="textarea"
      placeholder="配置说明"/>
    </el-form-item>

  <el-form-item label="配置内容">
    <el-input v-model="updateParmas.detail.Config"     
        :rows="30"
      type="textarea"
      placeholder="配置内容"/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handlerConfigUpdate">提交</el-button>
      <el-button @click="updateParmas.openFlag=false">取消</el-button>
    </el-form-item>
   </el-form>
  </div>

  </el-drawer>

  

</template>

<style scoped>

.pagination {
  float: right;
}

.right_button{
  float: right;
}
.box_create{
/*   padding: 3em; */
  width: 300px;
}

</style>